package com.chuangjiangx.microservice.eureka.consumer.controller;

import com.chuangjiangx.microservice.eureka.consumer.rest.DemoInterface;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

/**
 * 消费者控制器
 *
 * @author 马文韬
 * @version 1.0
 * @date: 2018/9/28
 */
@RestController
@RequestMapping("/consumer")
public class ConsumerController {

    @Autowired
    private RestTemplate restTemplate;

    @Autowired
    private DemoInterface demoInterface;

    @RequestMapping(value = "/hello", method = RequestMethod.GET)
    @ResponseBody
    @HystrixCommand(fallbackMethod = "helloFallback")
    public String hello() {
        return restTemplate.getForEntity("http://HELLO-DEMO/demo/hello", String.class).getBody();
    }

    @RequestMapping(value = "/hello/feign", method = RequestMethod.GET)
    @ResponseBody
    @HystrixCommand(fallbackMethod = "helloFallback")
    public String helloByFeign() {
        return demoInterface.hello();
    }

    public String helloFallback() {
        return "HELLO-DEMO service error!";
    }
}
